package com.aldjob.controllerImpl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.project.XXXX.api.SessionContext;
import com.project.XXXX.identification.tool.ValidateTool;
import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.stringmodel.EEinfoAndjobString;
import com.project.XXXX.stringmodel.JobEnrollString;
import com.project.XXXX.stringmodel.PtJobForShown;
import com.project.XXXX.stringmodel.PtJobForShownWithBlobs;
import com.project.XXXX.tools.ProjectTools;

public class EmployeePtMangaeController {

	private tbl_jobenrollMapper tbl_jobenrollMapper;
	private tbl_ptjobMapper tbl_ptjobMapper;
	private tbl_dictMapper tbl_dictMapper;

	public EmployeePtMangaeController(com.project.XXXX.inter.tbl_jobenrollMapper tbl_jobenrollMapper,
			com.project.XXXX.inter.tbl_ptjobMapper tbl_ptjobMapper,
			com.project.XXXX.inter.tbl_dictMapper tbl_dictMapper) {
		super();
		this.tbl_jobenrollMapper = tbl_jobenrollMapper;
		this.tbl_ptjobMapper = tbl_ptjobMapper;
		this.tbl_dictMapper = tbl_dictMapper;
	}

	public boolean canRequest(tbl_employeeinfo info) {
		if (info != null) {
			if (info.getUsername() == null)
				return false;

			if (info.getGender() == null)
				return false;

			if (info.getBirthday() == null)
				return false;

			if (info.getSchool() == null)
				return false;
			return true;
		}
		return false;
	}

	public int Request(int ptjobid, HttpSession sess) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_employeeinfo info = (tbl_employeeinfo) sess.getAttribute("EEinfo");
			if (canRequest(info) == false) {
				return 422;
			} else {
				tbl_jobenroll record = null;
				map.put("mobile", tbl_sysuser.getMobile());
				map.put("ptjobid", ptjobid);
				tbl_jobenroll t = tbl_jobenrollMapper.selectByPtidandmobile(map);
				if (t == null) {// success
					record = new tbl_jobenroll();
					record.setPtjobid(ptjobid);
					record.setHappendate(new Date());
					record.setStateid(30);
					record.setMobile(tbl_sysuser.getMobile());
					tbl_jobenrollMapper.insert(record);
					return 200;
				} else
					return 409;
			}
		} else {
			return 403;
		}
	}

	public List<JobEnrollString> getbyStatus(int page, String status, HttpSession sess) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<JobEnrollString> l = null;
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			int pageSize = 10;
			int currentPage = page;
			currentPage = (currentPage - 1) * pageSize;
			//System.out.println(status + "sfa");
			if (!StringUtils.isEmpty(status))
				map.put("status", status);
			map.put("pagesize", pageSize);
			map.put("mobile", tbl_sysuser.getMobile());
			map.put("offset", currentPage);

			//System.out.println(map);
			List<tbl_jobenroll> listJob = tbl_jobenrollMapper.getDiffReqJob(map);
			System.out.println(listJob.size()+"   "+tbl_jobenrollMapper.getDiffReqJobCount(map));
			l = new ArrayList<JobEnrollString>();
			for (tbl_jobenroll job : listJob) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				String salaryunit;
				tbl_ptjob tbl_ptjob = tbl_ptjobMapper.selectByPrimaryKey(job.getPtjobid());
				String time = sdf.format(tbl_ptjob.getBegindate()) + "~" + sdf.format(tbl_ptjob.getEnddate());
				salaryunit = tbl_dictMapper.selectByPrimaryKey(tbl_ptjob.getSalaryunit()).getValue();
				JobEnrollString jb = new JobEnrollString(job.getId() + "", sdf.format(job.getHappendate()),
						tbl_ptjob.getTitle(), tbl_ptjob.getSalary() + "", salaryunit, job.getPtjobid() + "",
						job.getMobile(), time);
				l.add(jb);
			}
		}
		return l;
	}

	public int getCountbyStatus(String status, HttpSession sess) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			map.put("mobile", tbl_sysuser.getMobile());
			if (!StringUtils.isEmpty(status))
				map.put("status", status);
			return tbl_jobenrollMapper.getDiffReqJobCount(map);
		} else
			return -1;

	}

	public int cancelRequest(int reqid, HttpSession sess) {
		if (sess != null) {
			tbl_sysuser tbl_sysuser = (tbl_sysuser) sess.getAttribute("User");
			tbl_jobenroll record = tbl_jobenrollMapper.selectByPrimaryKey(reqid);
			if (record != null) {
				int ptid = record.getPtjobid();
				if (ValidateTool.IsMyJob(tbl_sysuser, tbl_ptjobMapper, ptid)
						|| ValidateTool.IsJobEnroll(tbl_sysuser, tbl_jobenrollMapper, ptid))
					if (record.getStateid() != 33) {
						record.setStateid(33);
						record.setId(reqid);
						tbl_jobenrollMapper.updateByPrimaryKeySelective(record);
						return 200;
					}
				return 409;
			} else
				return 409;

		} else
			return 403;

	}
}